#include "ddscom.h"

//QString UPath;

DDSCOM::DDSCOM()
{
    //UPath = UDiskPath;
    //QString UPath = "0";
    //UDiskPath = UPath;
    UDiskPath = ":/";
    mcuConnect_flag = 0;
}

int DDSCOM::Device_connect(/*QString UDiskPath*/void)
{
    QString coePath = UDiskPath + "_sys/sys.coe";
    //QString coePath = "C:/sys.coe";
    QFile coeFile(coePath);
    if (!coeFile.open(QIODevice::ReadWrite/*|QIODevice::Text*/))
    {
        return -1;  //Open File Failure
    }
    QString comDat;
    comDat = coeFile.read(2);
    if(comDat == "00")
    {
        coeFile.reset();
        coeFile.write("10",2);
        coeFile.close();
        mcuConnect_flag = 1;
        return 0;
    }
    else
    {
        return -2;  //MCU is not running correctly
    }
}

int DDSCOM::Device_disconnect(/*QString UDiskPath*/void)
{
    QString coePath = UDiskPath + "_sys/sys.coe";
    //QString coePath = "C:/sys.coe";
    QFile coeFile(coePath);
    if (!coeFile.open(QIODevice::ReadWrite/*|QIODevice::Text*/))
    {
        return -1;  //Open File Failure
    }
    QString comDat;
    comDat = coeFile.read(2);
    if((comDat == "10") || (comDat == "00"))
    {
        coeFile.reset();
        coeFile.write("00",2);
        coeFile.close();
        mcuConnect_flag = 0;
        return 0;
    }
    else
    {
        return -2;  //MCU is not running correctly
    }
}

//int DDSCOM::UDisk_Find(QString UDiskPath)
QString DDSCOM::UDisk_Find(void)
{
    //QString UDiskPath;
    QString UDiskLabel;
    LPTSTR lpVolumeNameBuffer=new TCHAR[12];
    //QString lpVolumeNameBuffer;
    DWORD nVolumeNameSize=12;
    DWORD VolumeSerialNumber;
    DWORD MaximumComponentLength;
    LPTSTR lpFileSystemNameBuffer=new TCHAR[10];
    DWORD nFileSystemNameSize=10;
    DWORD FileSystemFlags;

    for(int i='D' ; i <= 'Z' ; i++)
    {
        UDiskPath.clear();
        UDiskPath.append(i);
        UDiskPath.append(":/");
        GetVolumeInformation((LPTSTR)UDiskPath.utf16(),
          lpVolumeNameBuffer, nVolumeNameSize,
          &VolumeSerialNumber, &MaximumComponentLength,
          &FileSystemFlags,
          lpFileSystemNameBuffer, nFileSystemNameSize);

        UDiskLabel.clear();
        UDiskLabel = QString::fromWCharArray(lpVolumeNameBuffer);
        //UDiskLabel = QString::fromUtf16(lpVolumeNameBuffer);
        //ui->textBrowser_infoOut->insertPlainText("Check in Disk :");
        //ui->textBrowser_infoOut->insertPlainText(UDiskPath + UDiskLabel + "\n");

        if(UDiskLabel == "BEAN_FLASH")
        {
            //ui->textBrowser_infoOut->insertPlainText("Device Found in " + UDiskPath + "\n");
            //break;
            UDiskPath.clear();
            UDiskPath.append(i);
            UDiskPath.append(":/");
            return UDiskPath;
        }
    }
    return "0";
}

int DDSCOM::sendPara(/*QString UDiskPath, char paradat[]*/void)
{
    QString pacPath = UDiskPath + "_sys/sys.pac";
    //QString pacPath = "C:/sys.pac";
    QFile pacFile(pacPath);
    if (!pacFile.open(QIODevice::ReadWrite/*|QIODevice::Text*/))
    {
        return -1;  //Open File Failure
    }
    //write pac file
    if(pacFile.write(pacdat.transfer,PAC_TRANS_SIZE) == -1)
    {
        pacFile.close();
        return -2;  //write data fail
    }
    pacFile.close();
    return 0;
}
